<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-20
 * 版    本：1.0.0
 * 功能说明：用户控制器。
 *
 **/

namespace Admin\Controller;

class SignController extends ComController
{

    public function index()
    {   
        $model = new \Think\Model();
        $mysql = $model->query("select VERSION() as mysql");
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $sign = M('sign_record');
        $prefix = C('DB_PREFIX');

        //获得当前登录驾校id
        $d_id = $this->USER['uid'];
        $l_type = I('l_type') ?  I('l_type') : 0 ;
        $c_id = I('c_id') ?  I('c_id') : 0 ;
        $is_paid = isset($_GET['is_paid']) ? $_GET['is_paid'] : 'n';
        $where = array();
        $title = '';
        if($d_id){
            $where["{$prefix}sign_record.d_id"]= $d_id;
        }
        if($l_type && !$c_id){
            $l_where['c.d_id'] = $d_id;
            $l_where['l.id'] = $l_type;
            $license_type = M('license_type');
            $ids = $license_type->alias('as l ')->field('c.*')->join("driving_class_level as c on l.type=c.type")->where($l_where)->select();
            foreach ($ids as $key => $value) {
                $title = $value['type'];
                $id[] = $value['id'];
            }
            $where["{$prefix}sign_record.c_id"] = array('in',$id);
        }
        if($c_id){
            $where["{$prefix}sign_record.c_id"] = $c_id;
            $c_data = M('class_level')->where('id = '.$c_id)->find();
            $title .= $c_data['type'];
            $title .= $c_data['name'];
        }
        if($is_paid !='n'){
            $where["{$prefix}sign_record.is_paid"] = $is_paid;
            if($is_paid == 0){
                 $title .= '未交费';
            }elseif($is_paid==1){
                 $title .= '已缴费';
            }
        }
        $title .='学员报名信息';

        $pagesize = 20; //每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $sign->where($where)->count();

        $list = $sign->where($where)
            ->field("{$prefix}sign_record.*,{$prefix}member.name as school_name,{$prefix}class_level.type,{$prefix}class_level.name as class_name")
            ->join("{$prefix}member ON {$prefix}member.uid = {$prefix}sign_record.d_id")
            ->join("{$prefix}class_level ON {$prefix}class_level.id = {$prefix}sign_record.c_id")
            ->order('id desc')
            ->limit($offset . ',' . $pagesize)
            ->select();

        $dayin = $sign->where($where)
            ->field("{$prefix}sign_record.*,{$prefix}member.name as school_name,{$prefix}class_level.type,{$prefix}class_level.name as class_name")
            ->join("{$prefix}member ON {$prefix}member.uid = {$prefix}sign_record.d_id")
            ->join("{$prefix}class_level ON {$prefix}class_level.id = {$prefix}sign_record.c_id")
            ->order('id desc')
            ->select();

         session('dayin',$dayin);
         session('title',$title);
        //驾照类型
        $license_type = M('license_type');
        $l_data = $license_type->where(array('d_id'=>$d_id))->select();
        $this->assign('license_type',$l_data);
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('mysql', $mysql[0]['mysql']);
        $this->assign('nav', array('', '', ''));//导航
        $this->display();
    }

    public function del()
    {

        $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;

        if ($ids) {
            if (is_array($ids)) {
                $aids = implode(',', $ids);
                $map['id'] = array('in', $ids);
            } else {
                $map = 'id=' . $ids;
            }

            if($comments = M("sign_record")->where($map)->delete()){
                $this->success('当前记录删除成功！');
            }else{
                $this->error('参数错误，请联系您的技术支持！');
            }
        } else {
            $this->error('您没有选择要删除的报名！');
        }
    }

    /**
     * [getClassLevel ajax 根据驾照类型获取班型信息] post
     * @return array    查询结果
     */
    public function getClassLevel(){
        $uid = session('uid');//当前登录获取驾校id
        $l_type = I('post.l_type') ? I('post.l_type') : 0;
        $where['c.d_id'] = $uid;
        $where['l.id'] = $l_type;
        // $where['l.id'] = 72;
        $license_type = M('license_type');
        $data = $license_type->alias('as l ')->field('c.*')->join("driving_class_level as c on l.type=c.type")->where($where)->select();
        if($data){
            echo json_encode(array('res'=>1,'data'=>$data,'msg'=>'ok'));die;
        }else{
            echo json_encode(array('res'=>0,'data'=>'','msg'=>'null'));die;
        }
    }

    public function dayin(){
        $data = session('dayin');
        $title = session('title');
        $this->assign('data',$data);
        $this->assign('title',$title);
        $this->display();
    }


}
